﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Diagnostics;

namespace SoftKeyLib
{
    /// <summary>
    /// 生成机器唯一编码
    /// </summary>
    public class SoftReg
    {
        #region 变量

        int[] intCode = new int[127]; // 存储密钥
        int[] intNumber = new int[25]; // 存机器码的ASCII值
        char[] Charcode = new char[25]; // 存储机器码字

        #endregion

        #region 私有方法

        /// <summary>
        /// 给数组赋值小于 10 的数
        /// </summary>
        private void setIntCode()
        {
            for (int i = 1; i < intCode.Length; i++)
            {
                intCode[i] = i % 9;
            }
        }

        #endregion

        #region 公开的方法

        /// <summary>
        /// 使用 ManagementObject 对象的 GetPropertyValue 方法获得本机的硬盘标识号【设备硬盘的卷标号】
        /// </summary>
        /// <returns></returns>
        public string GetDiskVolunmeSerialNumber()
        {
            try
            {
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObject disk = new ManagementObject("Win32_logicaldisk.deviceid=\"c:\"");
                disk.Get();

                return disk.GetPropertyValue("VolumeSerialNumber").ToString();
            }
            catch (Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif

                return ex.Message;
            }
        }

        /// <summary>
        /// 获取本机 CPU 序列号
        /// </summary>
        /// <returns></returns>
        public string GetCPU()
        {
            try
            {
                string strCpu = null;
                ManagementClass myCpu = new ManagementClass("Win32_Processor"); // 指定 win32_Processor 管理类
                ManagementObjectCollection myCpuConnection = myCpu.GetInstances();

                foreach (ManagementObject myObject in myCpuConnection)
                {
                    strCpu = myObject.Properties["Processorid"].Value.ToString();
                    break;
                }

                return strCpu;
            }
            catch (Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif

                return ex.Message;
            }
        }

        /// <summary>
        /// 生成机器码
        /// </summary>
        /// <returns></returns>
        public string GetMNum()
        {
            string strNum = GetCPU() + GetDiskVolunmeSerialNumber(); // 获得 24 位 CPU 和硬盘序列号
            string strMNum = strNum.Substring(0, 24); // 从生成的字符串中取出前 24 个字符作为机器码

            return strMNum;
        }

        /// <summary>
        /// 生成注册码【no use】
        /// </summary>
        /// <returns></returns>
        public string GetRNum()
        {
            setIntCode();
            for (int i = 1; i < Charcode.Length; i++)
            {
                Charcode[i] = Convert.ToChar(this.GetMNum().Substring(i - 1, 1));
            }
            for (int j = 1; j < intNumber.Length; j++)
            {
                intNumber[j] = intCode[Convert.ToInt32(Charcode[j])] + Convert.ToInt32(Charcode[j]);
            }

            string strAsciiName = ""; // 用于存储注册码
            for (int j = 1; j < intNumber.Length; j++)
            {
                if (intNumber[j] >= 48 && intNumber[j] <= 57) // 判断字符ASCII值是否在48-57之间
                {
                    strAsciiName += Convert.ToChar(intNumber[j]);
                }
                else if (intNumber[j] >= 65 && intNumber[j] <= 90) // 判断字符ASCII值是否在A-Z之间
                {
                    strAsciiName += Convert.ToChar(intNumber[j]);
                }
                else if (intNumber[j] >= 97 && intNumber[j] <= 122) // 判断字符ASCII值是否在a-z之间
                {
                    strAsciiName += Convert.ToChar(intNumber[j]);
                }
                else
                {
                    if (intNumber[j] > 122) // 判断字符ASCII值是否大于z
                    {
                        strAsciiName += Convert.ToChar(intNumber[j] - 10);
                    }
                    else
                    {
                        strAsciiName += Convert.ToChar(intNumber[j] - 9);
                    }
                }
            }

            return strAsciiName;
        }

        #endregion
    }
}
